package blog_grpc

import (
	"gitee.com/yangwencheng/vblog/apps/blog"
	"gitee.com/yangwencheng/vblog/apps/blog/blog_grpc/pb"
	"gitee.com/yangwencheng/vblog/ioc"
	"google.golang.org/grpc"
)

var _ ioc.GrpcIocObj = &Handler{}
var _ pb.BlogRpcServiceServer = &Handler{}

type Handler struct {
	srv blog.Service
	pb.UnimplementedBlogRpcServiceServer
}

func (h *Handler) Name() string {
	return blog.MODEL_NAME
}

func (h *Handler) Init() error {
	// 结构体之前的依赖关系 必须在Init 完成
	// 如果在init函数执行的话,很可能因为先后顺序导致空指针异常
	srv := ioc.App().GetController(blog.MODEL_NAME)
	h.srv = srv.(blog.Service)
	return nil
}

func NewHandler() *Handler {
	return &Handler{}
}

func (h *Handler) Register(s *grpc.Server) {
	pb.RegisterBlogRpcServiceServer(s, h)
}

func init() {
	ioc.App().SetGrpc(NewHandler())
}
